package leetcode.linkedlist;

public class RemoveNthNodeFromEndOfList19 {
      public class ListNode {
        int val;
      ListNode next;
         ListNode() {}
         ListNode(int val) { this.val = val; }
         ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     }

    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            if (head == null || n < 1) {
                return head;
            }
            ListNode curry = new ListNode(-1, head);
            ListNode fast = head;
            for (int i = 0; i < n; i++) {
                fast = fast.next;
            }
            ListNode temp = curry;
            while (fast != null) {
                fast = fast.next;
                temp = temp.next;
            }
            temp.next = temp.next.next;
            return curry.next;
        }
    }
}
